package com.gt.self.controller;

import com.gt.common.AjaxResult;
import com.gt.common.PrintUtil;
import com.gt.self.domain.vo.ReportInfoVo;
import com.gt.self.domain.vo.ReportParamVo;
import com.gt.self.service.SelfMachineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;

@Slf4j
@Api(tags = "自助报道功能")
@RestController
@RequestMapping("self/report")
//@CrossOrigin
public class SelfMachineReportController {

    @Autowired
    private SelfMachineService selfMachineService;

    /**
     * 三种情况：申请单号、身份证、就诊卡号
     *
     * @param commonNo
     * @return
     */
    @ApiOperation("自助报道接口")
    @GetMapping("selfServiceReport")
    public AjaxResult selfServiceReport(String commonNo) {
        AjaxResult reportResult = selfMachineService.selfServiceReport(commonNo);
        return reportResult;
    }

    @ApiOperation("打印小票接口")
    @PostMapping("printReportBill")
    public AjaxResult printReportBill(@RequestPart("file") MultipartFile file) throws IOException {

        File tempFile = null;
        FileOutputStream outputStream = null;
        try {
            tempFile = File.createTempFile("bill-", ".pdf");
            outputStream = new FileOutputStream(tempFile);
            IOUtils.copy(file.getInputStream(), outputStream);
            String path = tempFile.getPath();
            PrintUtil.PrintPdfByPath(path);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
//        // 将multipartFile内容转移到临时文件
//        String contentType = file.getContentType();
//        System.out.println(contentType);
//        String saveDir = "E:\\self\\";
//        Path path = Paths.get(saveDir).resolve(file.getOriginalFilename()+".pdf");
//        File dest = path.toFile();
//        file.transferTo(dest);

        return AjaxResult.success();
    }

    @ApiOperation("打印小票接口1")
    @PostMapping("printReportBill1")
    public AjaxResult printReportBill1(@RequestPart("file") MultipartFile file) throws IOException {
        File tempFile = null;
        FileOutputStream outputStream = null;
        tempFile = File.createTempFile("bill-", ".pdf");
        outputStream = new FileOutputStream(tempFile);
        IOUtils.copy(file.getInputStream(), outputStream);
        String path = tempFile.getPath();
        PrintUtil.PrintPdfByPath(path);
        return AjaxResult.success();
    }
}